package com.ruoyi.plus.pub.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.api.BaseApi;
import com.ruoyi.common.core.api.IdsEntity;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.plus.pub.api.po.PubBannerPo;
import com.ruoyi.plus.pub.api.po.PubBannerQueryPo;
import com.ruoyi.plus.pub.api.vo.PubBannerVo;
import com.ruoyi.plus.pub.domain.PubBanner;
import com.ruoyi.plus.pub.service.IPubBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统轮播图Api
 */
@Slf4j
@Api(tags = "PubBannerApi-系统轮播图")
@RestController
@RequestMapping("app/api/pub/pubBannerApi")
public class PubBannerApi extends BaseApi {

    @Autowired
    private IPubBannerService pubBannerService;

    /**
     * 获取系统轮播图对象
     */
    @ApiOperation("get-获取系统轮播图对象")
    @GetMapping("/get/{id}")
    @ResponseBody
    public ApiResult<PubBannerVo> get(@PathVariable("id") Long id) {
        PubBanner entity = pubBannerService.getById(id);
        return buildGetApiResult(id, entity, PubBannerVo.class);
    }


    /**
     * 查询系统轮播图列表
     */
    @ApiOperation("list-获取系统轮播图列表")
    @PostMapping("/list")
    @ResponseBody
    public ApiResult<PageInfo<PubBannerVo>> list(@RequestBody PubBannerQueryPo pubBannerQueryPo) {
        PubBanner pubBanner = this.buildCopyClass(pubBannerQueryPo, PubBanner.class);
        PageHelper.startPage(pubBannerQueryPo.getPageNum(), pubBannerQueryPo.getPageSize());
        List<PubBanner> list = pubBannerService.selectPubBannerList(pubBanner);
        return buildListApiResult(list, PubBannerVo.class);
    }

    /**
     * 新增保存系统轮播图
     */
    @ApiOperation("新增保存系统轮播图")
    @Log(title = "add-系统轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public ApiResult<Boolean> addSave(@RequestBody PubBannerPo pubBannerPo) {
        PubBanner pubBanner = buildCopyClass(pubBannerPo, PubBanner.class);
        return new ApiResult<Boolean>().setData(pubBannerService.save(pubBanner));
    }

    /**
     * 修改保存系统轮播图
     */
    @ApiOperation("修改保存系统轮播图")
    @Log(title = "系统轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public ApiResult<Boolean> editSave(@RequestBody PubBannerPo pubBannerPo) {
        PubBanner pubBanner = buildCopyClass(pubBannerPo, PubBanner.class);
        return new ApiResult<Boolean>().setData(pubBannerService.saveOrUpdate(pubBanner));
    }

    /**
     * 删除系统轮播图
     */
    @ApiOperation("remove-删除系统轮播图")
    @Log(title = "系统轮播图", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public ApiResult<Boolean> remove(@RequestBody IdsEntity idsEntity) {
        return new ApiResult<Boolean>().setData(pubBannerService.removeByIds(Arrays.asList(Convert.toLongArray(idsEntity.getIds()))));
    }

}
